/*
 * UINuevaConstanciaHabilitacion.java
 *
 * Created on 9 de enero de 2008, 12:54
 */

package vista;

import Datos.Consulta;
import controlador.CListar;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import negocios.Configuracion;
import negocios.EntidadExterna;
import negocios.Matriculado;
import negocios.Titulo;
import reportes.cExport_thread;

/**
 *
 * @author  Administrador
 */
public class UINuevaConstanciaHabilitacion extends javax.swing.JFrame
{
    private Collection entidades = new ArrayList();
    private Collection matriculados = new ArrayList();
    
    /** Creates new form UINuevaConstanciaHabilitacion */
    public UINuevaConstanciaHabilitacion()
    {
        initComponents();
        // Codigo necesario para centrar la ventana
                Dimension pantalla, cuadro;
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
                
		cuadro = this.getSize();
		this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
                this.llenarTabla(new EntidadExterna());
        //Fin del centrado
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jTMatriculaSolicitante = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jTNombreSolicitante = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLFoto = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jTDni = new javax.swing.JTextField();
        jTFechaInscripcion = new javax.swing.JTextField();
        jTLibro = new javax.swing.JTextField();
        jTFolio = new javax.swing.JTextField();
        jBBuscar = new javax.swing.JButton();
        jLabel21 = new javax.swing.JLabel();
        jTTitulo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jTBuscar = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jTNombreEntidad = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jBGenerar = new javax.swing.JButton();
        jBSalir = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel22 = new javax.swing.JLabel();
        jTfechaHasta = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nueva Constancia de Habilitacion");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del matriculado"));

        jLabel6.setBackground(new java.awt.Color(255, 255, 204));
        jLabel6.setText("Matricula");
        jLabel6.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel6.setOpaque(true);

        jTMatriculaSolicitante.setToolTipText("Introduzca una matricula existente.");
        jTMatriculaSolicitante.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTMatriculaSolicitanteKeyPressed(evt);
            }
        });

        jLabel15.setBackground(new java.awt.Color(255, 255, 204));
        jLabel15.setText("Nombre");
        jLabel15.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel15.setOpaque(true);

        jTNombreSolicitante.setEditable(false);
        jTNombreSolicitante.setToolTipText("Nombre del Matriculado");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Foto"));

        jLFoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLFoto.setText("Foto 4x4");
        jLFoto.setToolTipText("Foto del Matriculado");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLFoto, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLFoto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );

        jLabel16.setBackground(new java.awt.Color(255, 255, 204));
        jLabel16.setText("D.N.I");
        jLabel16.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel16.setOpaque(true);

        jLabel17.setBackground(new java.awt.Color(255, 255, 204));
        jLabel17.setText("Fecha Inscripcion");
        jLabel17.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel17.setOpaque(true);

        jLabel18.setBackground(new java.awt.Color(255, 255, 204));
        jLabel18.setText("Libro");
        jLabel18.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel18.setOpaque(true);

        jLabel19.setBackground(new java.awt.Color(255, 255, 204));
        jLabel19.setText("Folio");
        jLabel19.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel19.setOpaque(true);

        jTDni.setEditable(false);
        jTDni.setToolTipText("DNI del Matriculado");

        jTFechaInscripcion.setEditable(false);
        jTFechaInscripcion.setToolTipText("Fecha de Inscripcion del Matriculado");

        jTLibro.setEditable(false);
        jTLibro.setToolTipText("Libro de inscripcion del Matriculado");

        jTFolio.setEditable(false);
        jTFolio.setToolTipText("Folio de inscripcion del Matriculado");

        jBBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/binoculars.gif"))); // NOI18N
        jBBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBBuscarMouseClicked(evt);
            }
        });
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jLabel21.setBackground(new java.awt.Color(255, 255, 204));
        jLabel21.setText("Titulo");
        jLabel21.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel21.setOpaque(true);

        jTTitulo.setEditable(false);
        jTTitulo.setToolTipText("Folio de inscripcion del Matriculado");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                        .addComponent(jLabel18, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTMatriculaSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jBBuscar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTDni)
                            .addComponent(jTFolio)
                            .addComponent(jTFechaInscripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jTLibro, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jTTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                        .addGap(10, 10, 10)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTNombreSolicitante))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTMatriculaSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jBBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jTNombreSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTDni, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTFechaInscripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTLibro, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTFolio, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(11, 11, 11))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de la entidad"));

        jTBuscar.setToolTipText("Introduzca las primeras letras de la entidad para filtar el listado");
        jTBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTBuscarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTBuscarKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTBuscarKeyTyped(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 255, 204));
        jLabel1.setText("Buscar Entidad:");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setOpaque(true);

        jLabel20.setBackground(new java.awt.Color(255, 255, 204));
        jLabel20.setText("Nombre");
        jLabel20.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel20.setOpaque(true);

        jTNombreEntidad.setEditable(false);
        jTNombreEntidad.setToolTipText("Entidad Seleccionada");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setToolTipText("Seleccione un elemento.");
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel20, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTNombreEntidad, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                            .addComponent(jTBuscar))))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTNombreEntidad, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jTBuscar});

        jBGenerar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/printer.gif"))); // NOI18N
        jBGenerar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBGenerarMouseClicked(evt);
            }
        });
        jBGenerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGenerarActionPerformed(evt);
            }
        });
        jBGenerar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBGenerarKeyPressed(evt);
            }
        });

        jBSalir.setText("Salir");
        jBSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalirActionPerformed(evt);
            }
        });

        jLabel22.setBackground(new java.awt.Color(255, 255, 204));
        jLabel22.setText("Habilitado hasta:");
        jLabel22.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 255, 102), null));
        jLabel22.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(153, Short.MAX_VALUE)
                .addComponent(jBGenerar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(132, 132, 132))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator1)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel22, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTfechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTfechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBGenerar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBSalirActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jBSalirActionPerformed
    {//GEN-HEADEREND:event_jBSalirActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBSalirActionPerformed

    private void jTBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTBuscarKeyReleased
        // TODO add your handling code here:
        this.jTBuscar.setText(this.jTBuscar.getText().trim().toUpperCase());
        this.filtrar(this.jTBuscar.getText().trim());
    }//GEN-LAST:event_jTBuscarKeyReleased

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // cuando se seleccione una fila, los datos de esa fila deben aparecer en los textbox de abajo solo si no se desea guardar un nuevo elemento
       
            Iterator it = entidades.iterator();
            EntidadExterna e = new EntidadExterna();
            while(it.hasNext()) {
                e = (EntidadExterna)it.next();
                if(e.getIdentidadExterna() == Integer.parseInt(String.valueOf(this.jTable1.getValueAt(this.jTable1.getSelectedRow(),0)))) {
                    //this.jTCodigo.setText(String.valueOf(e.getIdentidadExterna()).trim());
                    this.jTNombreEntidad.setText(e.getNombre().trim());
                }
            }
       
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
    }//GEN-LAST:event_jTable1KeyPressed

    private void jBBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBBuscarMouseClicked
        // TODO add your handling code here:
        
       
}//GEN-LAST:event_jBBuscarMouseClicked

    private void jBGenerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGenerarActionPerformed
        // TODO add your handling code here:
            boolean imprimi=true;
            
            if (this.jTTitulo.getText().trim().equals("")){
                JOptionPane.showMessageDialog(this, "El matriculado no tiene un titulo asociado\nasignele un titulo en el registro del matriculado","No se puede Imprimir", JOptionPane.WARNING_MESSAGE);
                imprimi=false;
            }
            if (this.jTNombreEntidad.getText().trim().equals("")){
                JOptionPane.showMessageDialog(this, "Asegurese de Seleccionar una entidad y que el numero de matricula exista","No se puede Imprimir", JOptionPane.WARNING_MESSAGE);
                imprimi=false;
            }
            if (this.jTMatriculaSolicitante.getText().trim().equals("")){
                JOptionPane.showMessageDialog(this, "Asegurese de que la matricula este\nescrita en el campo 'Matricula Solicitante'","No se puede Imprimir", JOptionPane.WARNING_MESSAGE);
                imprimi=false;
            }
            if (imprimi){
                int res= JOptionPane.showConfirmDialog(null,"Desea Imprimir el carnet con foto?,Seleccione NO para imprimir elcarnet sin foto", "Imprimir Carnet",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if (res==JOptionPane.YES_OPTION){
                    
                    Matriculado m=new Matriculado(); 
                    m.setMatricula(Integer.parseInt(this.jTMatriculaSolicitante.getText().trim()));

                    
                    CListar listamat=new CListar();
                    Matriculado ma=listamat.soloMatriculado(m); 
                    String rutaImagen=this.obtenerRuta(ma.getMatricula());
                    
//                    System.out.println("ruta:"+rutaImagen+"--Mat:"+String.valueOf(ma.getMatricula())+"Entidad:"+this.jTNombreEntidad.getText().trim());
                    
                    cExport_thread report = new cExport_thread(3211,rutaImagen,String.valueOf(ma.getMatricula()),this.jTNombreEntidad.getText().trim(),this.jTfechaHasta.getText().trim());
                    report.start();
                }else{
                    cExport_thread report = new cExport_thread(322,this.jTMatriculaSolicitante.getText().trim(),this.jTNombreEntidad.getText().trim(),this.jTfechaHasta.getText().trim());
                    report.start();
                }
                
            }
    }//GEN-LAST:event_jBGenerarActionPerformed

    private void jTBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTBuscarKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();        
    }//GEN-LAST:event_jTBuscarKeyPressed

    private void jTBuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTBuscarKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jTBuscarKeyTyped

    private void jBGenerarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBGenerarMouseClicked
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jBGenerarMouseClicked

    private void jTMatriculaSolicitanteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTMatriculaSolicitanteKeyPressed
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        boolean numeroCorrecto=true;
        if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_ENTER) ||(c == KeyEvent.VK_DELETE)  ||(c == KeyEvent.VK_ESCAPE))))
        {    
            getToolkit().beep();
            JOptionPane.showMessageDialog(null,"Debe introducir un valor valido ej: 12","Atencion",JOptionPane.ERROR_MESSAGE);
            this.jTMatriculaSolicitante.setText("");
            this.jTMatriculaSolicitante.requestFocus();
            numeroCorrecto=false;
            evt.consume();
        }
        if(evt.getKeyCode()==27)
            this.dispose();
        if(evt.getKeyChar()==10 && numeroCorrecto)
            this.buscarMatriculado();                    
    }//GEN-LAST:event_jTMatriculaSolicitanteKeyPressed

    private void jBGenerarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBGenerarKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==27)
            this.dispose();
         
    }//GEN-LAST:event_jBGenerarKeyPressed

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        // TODO add your handling code here:
         this.buscarMatriculado();
    }//GEN-LAST:event_jBBuscarActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new UINuevaConstanciaHabilitacion().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBBuscar;
    private javax.swing.JButton jBGenerar;
    private javax.swing.JButton jBSalir;
    private javax.swing.JLabel jLFoto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTBuscar;
    private javax.swing.JTextField jTDni;
    private javax.swing.JTextField jTFechaInscripcion;
    private javax.swing.JTextField jTFolio;
    private javax.swing.JTextField jTLibro;
    private javax.swing.JTextField jTMatriculaSolicitante;
    private javax.swing.JTextField jTNombreEntidad;
    private javax.swing.JTextField jTNombreSolicitante;
    private javax.swing.JTextField jTTitulo;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTfechaHasta;
    // End of variables declaration//GEN-END:variables
    

    
private void buscarMatriculado(){
    if (this.jTMatriculaSolicitante.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Agregue un numero en Matricula Solicitante para buscarla","No se puede Buscar", JOptionPane.WARNING_MESSAGE);
        } else {
            this.limpiarCampos();
            Matriculado m=new Matriculado(); 
            m.setMatricula(Integer.parseInt(this.jTMatriculaSolicitante.getText().trim()));

            CListar listamat=new CListar();
            Matriculado ma=listamat.soloMatriculado(m);        
            
            if (ma.getMatricula()!=0){
                if(ma.getNombres()!=null){
                    this.jTNombreSolicitante.setText(ma.getApellido().trim()+", "+ma.getNombres().trim());
                }
                if(ma.getDni()!=null){
                        this.jTDni.setText(ma.getDni().trim());        
                }
                if(ma.getLibro()!=null){
                    this.jTLibro.setText(ma.getLibro().trim());
                }
                if(ma.getFolio()!=null){
                    this.jTFolio.setText(ma.getFolio().trim());
                }
                if (ma.getDiaIns()!=0.0 && ma.getMesIns()!=0.0 && ma.getAnioIns()!=0.0){
                    this.jTFechaInscripcion.setText(String.valueOf(String.valueOf(Math.round(ma.getDiaIns())))+"/"+String.valueOf(String.valueOf(Math.round(ma.getMesIns())))+"/"+String.valueOf(String.valueOf(Math.round(ma.getAnioIns()))));
                }        
                Titulo tit=ma.getTitulo();
                if (tit.getAbreviacion()!=null){
                    this.jTTitulo.setText(tit.getAbreviacion().trim());
                } 
                
                this.buscarUltimoPago(ma);
                
                
                this.jLFoto.setText("");

                CListar listaFoto=new CListar();
                String url=this.obtenerRuta(ma.getMatricula());                
                boolean fotoExiste = listaFoto.obtenerfoto(url, String.valueOf(ma.getMatricula()));

                if (fotoExiste){
                    File file = new File(url);
                    ImageIcon i = new ImageIcon(file.getPath());
                    ImageIcon tmpIcon = new ImageIcon(i.getImage().getScaledInstance(this.jLFoto.getWidth(), this.jLFoto.getHeight(), Image.SCALE_DEFAULT));
                    this.jLFoto.setIcon(tmpIcon);                      
                } else {
//                    System.out.println("no existe"+fotoExiste);
                }
            } else {
                JOptionPane.showMessageDialog(this, "Ingrese un numero de matricula existente","Matricula Inexistente", JOptionPane.WARNING_MESSAGE);
            }
        } 
}

public void buscarUltimoPago(Matriculado matri)
    {
        CListar listar = new CListar();
        
        Date ultimafecha = listar.ultimaFechaEncabezadoPago(matri.getIdmatriculado());
        
//        System.out.println("fehca obtenida de clistar :"+ultimafecha);
        
        if(ultimafecha != null){
            Calendar cal = Calendar.getInstance();
            cal.setTime(ultimafecha);
            
            if(cal.get(Calendar.MONTH) == 1) cal.set(Calendar.DAY_OF_MONTH, 28);
                else cal.set(Calendar.DAY_OF_MONTH,30);
            
            this.jTfechaHasta.setText((cal.get(Calendar.DAY_OF_MONTH))+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR));
        }else{
            int respu=JOptionPane.showConfirmDialog(this,"El matriculado no tiene cuotas pagadas\npor lo tanto no esta habilitado, desea expedirle igualmente una constancia?","Atencion",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
            if (respu==JOptionPane.YES_OPTION){
                JOptionPane.showMessageDialog(this, "Debera ingresar una fecha de habilitacion hasta manualmente", "Atencion, esta ingresando una fecha manual", JOptionPane.WARNING_MESSAGE);
            }
        }
        listar = null;
    }
 private void llenarTabla(EntidadExterna e)
    {
        CListar listar = new CListar();
        this.entidades = listar.hacerListado(e);
        listar = null;
        Iterator it = entidades.iterator();
        // ahora obtengo el modelo de la tabla
        DefaultTableModel modelo = (DefaultTableModel)this.jTable1.getModel();
        String datos[] = new String[2];
        while(it.hasNext())
        {
            e = (EntidadExterna)it.next();
            datos[0] = String.valueOf(e.getIdentidadExterna());
            datos[1] = e.getNombre();
            modelo.addRow(datos);
        }
    }
    private void filtrar(String cadena)
    {
        // de alguna forma mando la cadena para que otro objeto me devuelva una nueva coleccion con los barrios filtrados
        //barrios = algo(cadena);
        // solo queda limpiar la tabla y volverla a cargar
        this.limpiar();
        EntidadExterna e = new EntidadExterna();
        e.setNombre(cadena);
        this.llenarTabla(e);
    }
    private void limpiar()
    {
        DefaultTableModel modelo = (DefaultTableModel)this.jTable1.getModel();
        while(this.jTable1.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }
    private void limpiarCampos(){
        this.jTDni.setText("");
        this.jTFechaInscripcion.setText("");
        this.jTFolio.setText("");
        this.jTLibro.setText("");
        this.jTNombreSolicitante.setText("");
        this.jTTitulo.setText("");
        this.jTfechaHasta.setText("");
        this.jLFoto.setIcon(null);
    }
    private String obtenerRuta(int mat){
        Configuracion conf=new Configuracion();
         CListar listaConf=new CListar();    
        Configuracion c=listaConf.hacerListado(conf);        
        String destino="";
        if (c.getHost().trim().equals("localhost")||c.getHost().trim().equals("127.0.0.1")){
            destino=c.getDirectorioTemp().trim()+"\\\\";
        }else{
            destino="\\\\\\"+"\\"+c.getHost().trim()+"\\\\"+c.getDirectorioTemp().trim()+"\\\\";
        }
        String rutaImagen=destino+String.valueOf(mat)+".jpg";
        return rutaImagen;
    }
    
}

